import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/manager/xindong_notice_ctr.dart';
import 'package:xindong/manager/xindong_user_controller.dart';
import 'package:xindong/res/assets_res.dart';
import 'package:xindong/routes/tab_chats/controller/xindong_chats_tab_ctr.dart';
import 'package:xindong/routes/tab_chats/controller/xindong_liaotian_vm.dart';
import 'package:xindong/routes/tab_chats/xindong_chats_yemian.dart';
import 'package:xindong/routes/tab_home/controller/xindong_home_tab_controller.dart';
import 'package:xindong/routes/tab_home/xindong_home_yemian.dart';
import 'package:xindong/routes/tab_matches/xindong_pipei_yemian.dart';
import 'package:xindong/routes/tab_mine/xindong_wode_yemian.dart';
import 'package:xindong/routes/tab_moments/controller/xindong_dongtai_tab_controller.dart';
import 'package:xindong/routes/tab_moments/xindong_dongtai_yemian.dart';
import 'package:xindong/routes/tab_bar/xindong_tab_bar_class.dart';
import 'package:xindong/utils/xindong_image_util.dart';
import 'package:xindong/widgets/xindong_count_red_dots.dart';

class XinDongTabBarController extends GetxController {
  static XinDongTabBarController get to => Get.find();

  // 当前内容下标
  final RxInt currentIndex = RxInt(0);

  // 分页控制器 - 左右滚动切换
  late PageController yemianController;

  @override
  void onInit() {
    super.onInit();
    yemianController = PageController(initialPage: currentIndex.value);
  }

  // 构造
  List<XinDongTabClass> get tabList {
    List<XinDongTabClass> list = [
      XinDongTabClass(
        title: '缘份',
        type: TabBarType.home,
        icon: AssetsRes.YUANFEN_NOR,
        selectIcon: AssetsRes.YUANFEN_SEL,
        yemian: const XinDongHomeYeMian(),
      ),
      XinDongTabClass(
        title: '动态',
        type: TabBarType.moments,
        icon: AssetsRes.DONGTAI_NOR,
        selectIcon: AssetsRes.DONGTAI_SEL,
        yemian: const XinDongDongTaiYeMian(),
      ),
      XinDongTabClass(
        title: '速配',
        type: TabBarType.matches,
        icon: AssetsRes.SUPEI_NOR,
        selectIcon: AssetsRes.SUPEI_SEL,
        yemian: const XinDongPiPeiYeMian(),
      ),
      XinDongTabClass(
        title: '聊天',
        type: TabBarType.chats,
        icon: AssetsRes.LIAOTIAN_NOR,
        selectIcon: AssetsRes.LIAOTIAN_SEL,
        yemian: const XinDongChatsYeMian(),
      ),
      XinDongTabClass(
        title: '我的',
        type: TabBarType.mine,
        icon: AssetsRes.WODE_NOR,
        selectIcon: AssetsRes.WODE_SEL,
        yemian: const XinDongWoDeYeMian(),
      ),
    ];
    return list;
  }

  List<Widget> get tabBarItemList {
    List dataList = tabList;
    double itemWidth = screenWidth / dataList.length;
    List<Widget> widgetList = dataList.map((model) {
      int index = dataList.indexOf(model);
      return GestureDetector(
        onTap: () => setIndex(model, index),
        behavior: HitTestBehavior.opaque,
        child: SizedBox(
          height: double.infinity,
          width: itemWidth,
          child: Stack(
            alignment: Alignment.center,
            clipBehavior: Clip.none,
            children: [
              Column(
                mainAxisAlignment: MainAxisAlignment.center,
                crossAxisAlignment: CrossAxisAlignment.center,
                children: [
                  ImageUtil.asset(currentIndex.value == index ? model.selectIcon : model.icon, width: 30, height: 30),
                  Text(
                    model.title,
                    style: TextStyle(
                      color: currentIndex.value == index ? DZColors.mainColor : DZColors.contentColor,
                      fontSize: 10,
                    ),
                  ),
                ],
              ),
              // 聊天
              if (model.type == TabBarType.chats)
                Obx(() => Positioned(
                      top: 2,
                      left: itemWidth / 2 + 8, // 看实际图标大小调整偏移
                      child: XinDongUnreadCountWidget(
                        count: XinDongLiaoTianVM.to.unreadCount.value + XinDongNoticeCtr.to.systemCount,
                        maxCount: 99,
                      ),
                    )),
            ],
          ),
        ),
      );
    }).toList();
    return widgetList;
  }

  void setIndex(XinDongTabClass model, int index) {
    if (index == currentIndex.value) {
      switch (tabList[index].type) {
        case TabBarType.home:
          XinDongHomeTabController.to.notifyRefresh();
          break;
        case TabBarType.moments:
          XinDongDongTaiTabController.to.notifyRefresh();
          break;
        case TabBarType.matches:
          break;
        case TabBarType.chats:
          XinDongChatsTabController.to.notifyRefresh();
          break;
        case TabBarType.mine:
          XinDongUserController.to.requestUserInfo();
          break;
        default:
          break;
      }
      return;
    } else {
      jump(index);
      if (tabList[index].type == TabBarType.mine) {
        XinDongUserController.to.requestUserInfo();
      } else if (tabList[index].type == TabBarType.chats) {
        XinDongNoticeCtr.to.messageUnreadCount();
      }
    }
  }

  // 跳转
  void jump(int index) {
    currentIndex.value = index;
    yemianController.jumpToPage(index);
  }
}
